package org.djw.org.service.impl;

import org.djw.basic.service.impl.BaseServiceImpl;
import org.djw.basic.util.PageList;
import org.djw.org.domain.Department;
import org.djw.org.mapper.DepartmentMapper;
import org.djw.org.query.DepartmentQuery;
import org.djw.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 部门模块业务层
 */
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService{
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public void add(Department department) {
        departmentMapper.save(department);
        this.update(department);
    }

    @Override
    public void update(Department department) {
        String dirPath = "";
        //如果父id为空,则为新增顶级
        if (department.getParent() == null){
            dirPath = "/"+department.getId();
        }else {
            //不为空则查询出上级部门+自己的id
            Department parDepartment = departmentMapper.loadById(department.getParent().getId());
            dirPath = parDepartment.getDirPath()+"/"+department.getId();
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }

    @Override
    public List<Department> findTree() {
        return departmentMapper.findTree();
    }
}
